package gameinfo;

/**
 * Provide the structure, access and updating of the top scores of the game.
 * @author quique
 */
public class TopScores {
        
        /**
         * Keeps the 5 top-scores.If there are less than 5 players, the last are null.
         * @see Score
         */
	private Score[] scores;
        
        /**
         * Constructor
         */
        public TopScores(){
            scores = new Score[5];
        }
	
        /**
         * 
         * @param position from 1 (top player) to 5 (the last top player)
         * @return the score in the position passed by parameter
         */
	public Score getScore(int position) {
		return scores[position-1];
	}
        
        /**
         * Position goes from 1 to 5 as maximu
         * @param score
         * @param position
         */
        public void addScore(Score score,int position){
            scores[position-1] = score;
        }
	
        /**
         * Update the topscores if the finalscore is upper than one of saved scores.
         * If not, the object has no modification.
         * @param userName
         * @param finalScore
         * @return true if changes happened, false if not
         */
	public boolean updateTopScores(String userName, int finalScore) {
            int i=1;
            while((i<6)&&(this.getScore(i)!=null)){
                int dif=0;
                dif = getScore(i).getScore()-finalScore;
                //if the dif is negative, then is necessary do changes in the list, 
                //if is equals or positive nothing will happen
                if(dif<0){
                    for(int j=i;j<6;j++){
                        //save former values
                        String auxUser = getScore(j).getName();
                        int auxScore = getScore(j).getScore();

                        //write new values
                        getScore(j).setName(userName);
                        getScore(j).setScore(finalScore);
                        
                        //former values into new values
                        userName = auxUser;
                        finalScore = auxScore;
                    }
                    break;
                }
                i++;
            }
            return true;
	}
	 
}
 
